home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’96 / Papers ’96 / Standard Template Library / Demo / macString.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-21  |  1.8 KB  |  41 lines  |  [TEXT/SPM ]

  1. #include <stl.h>
  2.  
  3. // The macString class stores a string and is accesible either as a Pascal string or as a c-string.
  4. // The data is stored in str, a vector<char> and also in parallel in a Pascal string.
  5. // Bugs:
  6. //    Should issue warnings when a macString with over 255 characters is accessed as a unsigned char *.
  7. //        as this could result is truncation and data loss.
  8. class macString{
  9. public:
  10.     macString() : bPStrOK(true), bCStrOK(true), s(256) {}
  11.     macString(const macString&);
  12.     macString(char c) : bPStrOK(false), bCStrOK(true), s(256) {s[0] = c; synchronize();}
  13.     macString(const char *s);
  14.     macString(const unsigned char *s);
  15.  
  16.     operator char *() {return str();}
  17.     operator const char *() {return conststr();}
  18.     operator unsigned char *() {return pstr();}
  19.     operator const unsigned char *() {return constpstr();}
  20.     friend ostream& operator<< (ostream& os, macString& ms) {os << ms.conststr(); return os;} 
  21.     friend ostream& operator<< (ostream& os, const macString& ms) {os << ms.conststr(); return os;}
  22.     friend operator< (macString& ms1, macString& ms2) {return strcmp(ms1.conststr(), ms2.conststr()) < 0;}
  23.     friend operator< (const macString& ms1, const macString& ms2) {return strcmp(ms1.conststr(), ms2.conststr()) < 0;}
  24.     friend operator== (macString& ms1, macString& ms2) {return !strcmp(ms1.conststr(), ms2.conststr());}
  25.     friend operator== (const macString& ms1, const macString& ms2) {return !strcmp(ms1.conststr(), ms2.conststr());}
  26.     macString& operator+=(const macString &);
  27.     friend macString operator+ (macString& ms0, macString& ms1);
  28.     macString& operator= (const macString&);
  29.     void dump(void);
  30. private:
  31.     bool bCStrOK;
  32.     bool bPStrOK;
  33.     vector<char> s;
  34.     Str255 str255;
  35.       const char* conststr() const;
  36.     char* str();
  37.     const unsigned char* constpstr();
  38.     unsigned char* pstr() {synchronize(); bCStrOK = false; return str255;}
  39.     void synchronize();
  40. };
  41.